package com.newtouch.bxzs.enterprise.controller;

import cn.hutool.core.bean.BeanUtil;
import com.newtouch.bxzs.common.base.em.PushCustomerEnum;
import com.newtouch.bxzs.common.base.model.PushCustomerRecord;
import com.newtouch.bxzs.enterprise.model.bo.EnterpriseCustomerDetailBO;
import com.newtouch.bxzs.enterprise.model.bo.PersonalCustomerBO;
import com.newtouch.bxzs.enterprise.model.bo.ReceiveCustomerBO;
import com.newtouch.bxzs.enterprise.model.bo.RevocationCustomerBO;
import com.newtouch.bxzs.enterprise.model.vo.GoBackCustomerVO;
import com.newtouch.bxzs.enterprise.model.vo.ReceivePushRecordVO;
import com.newtouch.bxzs.enterprise.model.vo.ReceiveRelationShipVO;
import com.newtouch.bxzs.enterprise.model.vo.UserRelationShipVO;
import com.newtouch.bxzs.enterprise.service.AcceptionCustomerService;
import com.newtouch.bxzs.enterprise.service.PersonalCustomerService;
import com.newtouch.bxzs.enterprise.service.PushCustomerService;
import com.newtouch.bxzs.enterprise.service.RelationShipService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 *
 * 外部接口
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/8 9:11</td>
 * <td>yuanjie</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author yuanjie
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/externalController")
public class ExternalController {

    private static final Log logger = LogFactory.getLog(ExternalController.class);

    @Autowired
    private AcceptionCustomerService acceptionCustomerService;

    @Autowired
    private PushCustomerService pushCustomerService;

    @Autowired
    private RelationShipService relationShipService;

    @Autowired
    private PersonalCustomerService personalCustomerService;

    @ApiIgnore
    @ApiOperation("接收客户推送记录")
    @PostMapping("/receivePushRecord")
    public boolean receivePushRecord(@RequestBody ReceivePushRecordVO receivePushRecordVO){
        logger.info("AcceptionCustomerController.receivePushRecord:接收客户推送记录");
        return acceptionCustomerService.receivePushRecord(receivePushRecordVO);
    }

    /**
     * 接收对端撤销客户推送消息
     * @param revocationCustomerBO 撤回信息
     * @return boolean
     */
    @ApiIgnore
    @PostMapping("/acceptRevocationCustomer")
    public boolean revocationCustomer(@RequestBody RevocationCustomerBO revocationCustomerBO){
        logger.info("AcceptionCustomerController.revocationCustomer:接收对端撤销客户推送消息");
        return acceptionCustomerService.revocationCustomer(revocationCustomerBO);
    }

    /**
     * 对端接收客户后，获取三方关系及接收情况反馈
     * @param receiveRelationShipVO
     *          三方关系反馈
     * @return boolean
     */
    @ApiIgnore
    @PostMapping("/acceptionCustomerRelation")
    public ReceiveCustomerBO acceptionCustomerRelation(@RequestBody ReceiveRelationShipVO receiveRelationShipVO){
        logger.info("执行PushCustomerController.acceptionCustomerRelation():三方关系反馈");
        ReceiveCustomerBO receiveCustomer = new ReceiveCustomerBO();
        if (!PushCustomerEnum.RECEIVED.getCode().equals(receiveRelationShipVO.getPushCustomerStatus())){
            boolean isSucceed = pushCustomerService.updatePushStatus(receiveRelationShipVO.getPushCustomerRecordId(),receiveRelationShipVO.getPushCustomerStatus(),false);
            receiveCustomer.setSuccessLogo(isSucceed);
            return receiveCustomer;
        }
        boolean isSucceed = pushCustomerService.updatePushStatus(receiveRelationShipVO.getPushCustomerRecordId(),receiveRelationShipVO.getPushCustomerStatus(),true);
        receiveCustomer.setSuccessLogo(isSucceed);
        if (!isSucceed){
            //修改失败返回失败标识
            return receiveCustomer;
        }
        PushCustomerRecord pushCustomerRecord = pushCustomerService.getPushRecordById(receiveRelationShipVO.getPushCustomerRecordId());
        if (!receiveRelationShipVO.getExistFlag()){
            //判断三方关系是否存在
            UserRelationShipVO userRelationShipVO = new UserRelationShipVO();
            BeanUtils.copyProperties(pushCustomerRecord,userRelationShipVO);
            receiveCustomer.setSuccessLogo(relationShipService.saveRelationShip(userRelationShipVO));
        }
        if (StringUtils.isEmpty(receiveRelationShipVO.getUnifiedSocialCreditCode())){
            //个人客户接收返回个人客户
            PersonalCustomerBO personalCustomerDetail = personalCustomerService.getPersonalCustomerDetail(receiveRelationShipVO.getCustomerId());
            if (!BeanUtil.isEmpty(personalCustomerDetail)){
                receiveCustomer.setPersonalCustomer(personalCustomerDetail);
            }
        }else {
            //企业客户接收返回企业客户
            EnterpriseCustomerDetailBO enterpriseCustomer = pushCustomerService.getEnterpriseCustomerByCode(receiveRelationShipVO.getUnifiedSocialCreditCode(),pushCustomerRecord.getUserJobNumber());
            if (!BeanUtil.isEmpty(enterpriseCustomer)){
                receiveCustomer.setEnterpriseCustomerDetail(enterpriseCustomer);
            }
        }
        return receiveCustomer;
    }

    @ApiIgnore
    @ApiOperation("客户退回")
    @PostMapping("/customerGoBack")
    public boolean customerGoBack(@RequestBody GoBackCustomerVO goBackCustomerVO){
        logger.info("执行PushCustomerController.receivePushRecord():接收客户退回");
        return acceptionCustomerService.customerGoBack(goBackCustomerVO);
    }
}
